{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove LED Bar example\n",
    "\n",
    "----\n",
    "* [Introduction](#Introduction)\n",
    "* [Setup the board](#Setup-the-board)\n",
    "* [Setup LED Bar](#Setup-LED-Bar)\n",
    "* [Use the LED Bar](#Use-the-LED-Bar)\n",
    "\n",
    "----\n",
    "## Introduction\n",
    "\n",
    "This example shows how to use the Grove LED bar on the PYNQ-Z1 or PYNQ-Z2 board.\n",
    "\n",
    "A [Grove LED Bar](http://www.seeedstudio.com/depot/Grove-LED-Bar-v20-p-2474.html) and Pynq Grove Adapter are required.\n",
    "\n",
    "The LED bar is an example of a peripheral driven by GPIO.\n",
    "\n",
    "The LED bar is attached to the G1 connection on the Pynq Grove Adapter, which is connected to PMODB on the board. \n",
    "\n",
    "The LED bar has 10 LEDs. 8 Green LEDs, 1 Orange LED, and 1 Red LED. The brightness for each LED can be set independently. All functions supporting the LED bar are implemented in the C code running on the IOP driving the LED bar. \n",
    "\n",
    "----\n",
    "## Setup the board"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup LED Bar\n",
    "1. Connect the ***pmod2grove*** to ***PMODB***. \n",
    "2. Connect ***LED Bar*** to port ***G1*** of the Pynq Grove Adapter.\n",
    "\n",
    "## Use the LED Bar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Instantiate and reset LED Bar\n",
    "Create an instance of the LED bar \n",
    "The IOP (Pmod IOP (A or B) or Arduino IOP must be selected, along with the pins on the Pynq Grove adapter. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.lib.pmod import Grove_LEDbar\n",
    "from pynq.lib.pmod import PMOD_GROVE_G1 # Import constants\n",
    "\n",
    "# Instantiate Grove LED Bar on PMODB and on Pmod2Grove G1\n",
    "# PMODA = 1, PMODB = 2, Arduino = 3\n",
    "# Pynq Grove Adapter; G1 = [0,4], G2 = [1,5], G3 = [7,3], G4 = [6,2]\n",
    "# The LED bar can be connected to G1-7 on the Pynq Arduino Shield:\n",
    "# G1 = [2,3], G2 = [3,4], G3 = [4,5], G4 = [6,7], G5 = [8,9], G6 = [10,11], G7 = [12,13]\n",
    "ledbar = Grove_LEDbar(base.PMODB,PMOD_GROVE_G1)\n",
    "ledbar.reset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Turn individual LEDs on or off\n",
    "\n",
    "Write a 10-bit binary pattern, with each bit representing the corresponding LED. 1 = on, 0 = off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from pynq.pmods import pmod_const\n",
    "from time import sleep\n",
    "\n",
    "# Light up different bars in a loop\n",
    "for i in range(2):\n",
    "    ledbar.write_binary(0b1010100000)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b0000100100)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b1010101110)\n",
    "    sleep(0.5)\n",
    "    ledbar.write_binary(0b1111111110)\n",
    "    sleep(0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Set LEDs individually with different brightness levels\n",
    "\n",
    "The brightness of each LED can be set individually by writing a list of 10x 8-bit values to the LED bar. 0 is off, 0xff is full brightness. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Brightness 0-255\n",
    "HIGH = 0xFF\n",
    "MED  = 0xAA\n",
    "LOW  = 0x01\n",
    "OFF  = 0X00\n",
    "\n",
    "brightness = [OFF, OFF, OFF, LOW, LOW, MED, MED, HIGH, HIGH, HIGH]\n",
    "\n",
    "ledbar.write_brightness(0b1111111111,brightness)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.  Set the \"level\" or the number of LEDs which are set \n",
    "\n",
    "A number or level of LEDs can be turned on, started from either end of the LED bar. For example, this could be used to indicate the level of something being measured.\n",
    "\n",
    "write_level(level, bright_level, green_to_red)\n",
    "\n",
    "* level is the number of LEDs that are on.\n",
    "* bright_level [0-10] is the level of brightness\n",
    "* green_to_red = 1 means the LEDs start being lit from the \"green\" end of the LED bar\n",
    "* green_to_red = 0 means the LEDs start being lit from the \"red\" end of the LED bar.\n",
    "\n",
    "e.g. ledbar.write_level(5,4,1) will light 5 LEDs, to brightness 4 (out of 10) and will start from the Green LED (the LED furthest away from Grove connector on the LED bar module.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range (1,11):\n",
    "    ledbar.write_level(i,3,0)\n",
    "    sleep(0.3)\n",
    "for i in range (1,10):\n",
    "    ledbar.write_level(i,3,1)\n",
    "    sleep(0.3)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. Simple example of controlling the LED Bar from the board buttons\n",
    "\n",
    "The \"level\" of the LEDs can be set using buttons on the board. \n",
    "\n",
    "* Button 0 to increase level\n",
    "* Button 1 to decrease level\n",
    "* Button 3 to exit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_BTNS = 4\n",
    "\n",
    "btns = [base.buttons[index] for index in range(4)] \n",
    "i = 0\n",
    "ledbar.reset()\n",
    "\n",
    "done = False\n",
    "while not done:\n",
    "    if btns[0].read() == 1:\n",
    "        sleep(0.2)\n",
    "        i = min(i+1,10)\n",
    "        ledbar.write_level(i,2,1)\n",
    "    elif btns[1].read() == 1:\n",
    "        sleep(0.2)\n",
    "        i = max(i-1,0)\n",
    "        ledbar.write_level(i,2,1)\n",
    "    elif btns[3].read() == 1:\n",
    "        ledbar.reset()\n",
    "        done = True"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
